<?php

namespace app\service;

class Category
{
    public static function getCategoryTree(array $data, int $pid = 0): array
    {
        $tree = [];
        foreach ($data as $item) {
            if ($item['pid'] == $pid) {
                $children = self::getCategoryTree($data, $item['id']);
                if (!empty($children)) {
                    $item['children'] = $children;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }
}